// TODO :
//  * Get all problems in the map area.

var map =
    {
	mapObj : null,
	news_log : null,
	markers: [],
	init : function (lat, lng) {
	    map.mapObj = new DMap("map",
		{
		    point:new DLatLng(lat, lng),

		    width: "468px",
		    contextMenu: true,
		    reduced_scale: true,
		    map_type: "TYPE_SKYVIEW",
		    level:2
		});

	    var zoom_ctrl = new DZoomControl();
	    var type_ctrl = new DMapTypeControl();
	    var mark = new DMark(new DPoint(lng, lat));
			      
	    map.mapObj.addOverlay(mark);
	    map.mapObj.addControl(zoom_ctrl);
	    map.mapObj.addControl(type_ctrl);
//	    map.mapObj.enableDoubleClickZoom();
	    zoom_ctrl.setValign("bottom");

	    var boundary = map.mapObj.getBound();
	    map.spreadMarker();
	    map.panoramio_image();
	    DEvent.addListener(map.mapObj, "enddrag", map.spreadMarker);
	},

	stripHTMLtag : function(string) {
	    var objStrip = new RegExp();
	    objStrip = /[<][^>]*[>]/gi;
	    return string.replace(objStrip, "");
	},

	panoramio_image : function() {
	    var boundary = map.mapObj.getBound();
	    $.getJSON("lib/panoramio_proxy.php?minx=" + boundary.left +
		      "&maxx=" + boundary.right +
		      "&miny=" + boundary.bottom +
		      "&maxy=" + boundary.top +
		      "&size=square",
		      function (data) {
			  var photos = data.photos;
			  var cssObj = {
			      'border': "1px solid gray",
			      'margin': "4px"
			  };

			  $.each(photos, function (i, item) {
			      var a = $("<a/>").attr('href', item.photo_url);
			      $("<img/>").attr('src', item.photo_file_url)
			      .css(cssObj).appendTo(a);
			      a.appendTo("#photos");
			  });
			  $("<div/>").css({
			      'float':'right',
			      'clear':'both',
			      'margin-bottom' : '10px'
				      }).html('Powered by <a href="http://panoramio.com">panoramio.com</a>').appendTo("#photos");

		      });
	},

	related_news : function(query) {
	    var qry = encodeURI(query);
	    if (query == map.news_log) {
		return;
	    }

	    map.news_log = query;
	    $("#news-list").empty();
	    $.getJSON("lib/naver_news_proxy.php?query=" + qry +
		      "&display=5",
		      function (data) {
			  $.each(data, function (i, item) {
			      var article = $("<div/>").addClass('news-article');

			      var title_link = $("<a/>").attr("href", item.link).html(map.stripHTMLtag(item.title)).css('color', 'blue');
			      var title = $("<h5/>");
			      title_link.appendTo(title);

			      var dt = Date.parse(item.pub_date);
			      var pdate = new Date(dt);

			      var year = pdate.getFullYear();
			      var month = Number(pdate.getMonth())+1;
			      var day = pdate.getUTCDate();

			      var h = pdate.getHours();
			      var hour = h < 10 ? "0" + h : h;
			      var min = pdate.getMinutes();
			      var sec = pdate.getSeconds();

			      var pub_date_str = year + "년 " + month + "월 " + day + "일 " +  hour + ":" + min;

			      var pub_date = $("<p/>").text(pub_date_str);
			      var desc = $("<div/>").html(map.stripHTMLtag(item.desc));
			      title.appendTo(article);
			      pub_date.appendTo(article);
			      desc.appendTo(article);
			      article.appendTo("#news-list");
			  });
		      });
	},

	spreadMarker : function() {
	    // map.mapObj.clearOverlay();
	    var boundary = map.mapObj.getBound();
	    $.getJSON("lib/get_mark_within_bound.php?left=" + boundary.left + 
		      "&right="  + boundary.right +
		      "&bottom=" + boundary.bottom +
		      "&top="    + boundary.top,
		      function (data) {
			  $.each(data, function (i, item) {
			      if ( typeof (map.markers[item.id]) == 'undefined' ||
				   map.markers[item.id] == false) {
				  var mark = new DMark(new DPoint(item.lng, item.lat),
				      { draggable: false});
				  map.mapObj.addOverlay(mark);
				  map.markers[item.id] = true;
			      }
			  });
		      });
	}
    };


var bindAndOpenDialog = function(id) {
    $("#hidden_update_id").attr('value', id);
    $("#hidden_update_details").text($("#update_details_" + id).text());
    $("#modify_update_dialog").dialog("open");
}